{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "from importlib import reload\n",
    "import fxcm_rest_api_token as fxcm_rest_api\n",
    "result = reload(fxcm_rest_api)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "trader = fxcm_rest_api.Trader('YOURKEYHERE', 'prod')\n",
    "trader.debug_level = \"INFO\" # verbose logging... don't set to receive errors only\n",
    "trader.login()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "c =trader.candles(\"EUR/USD\", \"m15\", 45, dt_fmt=\"%Y/%m/%d %H:%M:%S\")['candles']\n",
    "print(len(c))\n",
    "for candle in c:\n",
    "    print(candle)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "help(trader.open_trade)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "res = trader.open_trade(trader.account_id, \"USD/JPY\", True, 1)\n",
    "orderId = res['data']['orderId']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tradeId = trader.get_tradeId(orderId)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(tradeId)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "trader.close_all_for_symbol(\"USD/JPY\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def print_candles(candle_data):\n",
    "    candles = candle_data['candles']\n",
    "    headers = candle_data['headers']\n",
    "    print(\"{0[10]}, {0[1]}, {0[3]}, {0[4]}, {0[2]}\".format(headers))\n",
    "    candles.reverse()\n",
    "    for candle in candles:\n",
    "        print(\"{0[10]}, {0[1]:0.6}, {0[3]:0.6}, {0[4]:0.6}, {0[2]:0.6}\".format(candle))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print_candles(cd)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print_candles(trader.get_candles(\"EUR/USD\", \"m1\", 5, dt_fmt=\"%Y/%m/%d %H:%M:%S\"))\n",
    "# time.sleep(60)\n",
    "# print(\"*\"*30)\n",
    "# print_candles(trader.get_candles(\"EUR/USD\", \"m1\", 5, dt_fmt=\"%Y/%m/%d %H:%M:%aS\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "trader.candles_as_dict(\"EUR/USD\", \"m1\", 1, dt_fmt=\"%Y/%m/%d %H:%M:%S\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "trader.close_all_for_symbol(trader.account_id, True, \"USD/JPY\", \"AtMarket\", \"GTC\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "trader.accounts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "trader.properties"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(trader.account_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Show how to supply a different handler for subscription items"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\n",
    "def show(msg):\n",
    "    '''\n",
    "    Sample price handler. If on_price_update is registered for a symbol,\n",
    "    it will update the symbol's values (stored in a symbol hash) with\n",
    "    that price update.symbol hash.\n",
    "\n",
    "    :return: none\n",
    "    '''\n",
    "    try:\n",
    "        md = json.loads(msg)\n",
    "        symbol = md[\"Symbol\"]\n",
    "        t = trader\n",
    "        si = trader.symbol_info.get(symbol, {})\n",
    "        p_up = dict(symbol_info=t.symbol_info[symbol], parent=t)\n",
    "        t.symbols[symbol] = t.symbols.get(symbol, fxcm_rest_api\n",
    "                                          .PriceUpdate(p_up,\n",
    "                                                       symbol_info=si))\n",
    "        trader.symbols[symbol].bid, trader.symbols[symbol].ask,\\\n",
    "            trader.symbols[symbol].high,\\\n",
    "            trader.symbols[symbol].low = md['Rates']\n",
    "        trader.symbols[symbol].updated = md['Updated']\n",
    "        print(t.symbols[symbol])\n",
    "    except Exception as e:\n",
    "        trader.logger.error(\"Can't handle price update: \" + str(e))\n",
    "\n",
    "# trader = fx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "#trader.unsubscribe_symbol(\"EUR/USD\")\n",
    "trader.subscribe_symbol(\"EUR/USD\", handler=show)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "trader.unsubscribe_symbol(\"EUR/USD\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = trader.subscribe_symbol(\"GBP/JPY\")\n",
    "print(result)\n",
    "print(trader.subscriptions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(trader.symbols[\"GBP/JPY\"])\n",
    "print(trader.symbols[\"GBP/JPY\"].bid)\n",
    "print(trader.symbols[\"GBP/JPY\"].ask)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(trader.symbols['GBP/JPY'].parent)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "trader.unsubscribe_symbol(\"GBP/JPY\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "trader.open_trade(trader.account_id, \"USD/JPY\", True, 1, at_market=1, time_in_force=\"FOK\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
